﻿using System;
using System.Text;

namespace MyFastDataSourceBind
{
    /// <summary>
    /// 数据类型转换例程，编写 袁永福
    /// </summary>
    public class ValueTypeHelper
    {
        /// <summary>
        /// 将指定的数值按照指定的格式转换为要显示的文本
        /// </summary>
        /// <param name="v">数值</param>
        /// <param name="Format">格式</param>
        /// <returns>要显示的文本</returns>
        public static string GetDisplayText(object v, string Format)
        {
            if (v == null || DBNull.Value.Equals(v))
                return "";
            if ( Format != null && Format.Trim().Length > 0 )
            {
                if (v is System.IFormattable)
                {
                    return ((System.IFormattable)v).ToString(Format, null);
                }
            }
            if (v is float || v is decimal || v is double)
            {
                double dbl = Convert.ToDouble(v);
                string txt = dbl.ToString("0.0000");
                //char[] chs = txt.ToCharArray();
                int len = txt.Length - 1;
                for (int iCount = txt.Length - 1; iCount >= 0; iCount--)
                {
                    char c = txt[iCount];
                    if (c == '.')
                    {
                        len = iCount;
                        break;
                    }
                    if (c == '0')
                    {
                        len = iCount;
                    }
                    else
                    {
                        break;
                    }
                }
                if (txt.IndexOf('.') >= 0)
                {
                    txt = txt.Substring(0, len);
                }
                return txt;
            }
            if (v is DateTime)
                return ((DateTime)v).ToString("yyyy-MM-dd");
            return Convert.ToString(v);
        }
        
        ///// <summary>
        ///// 判断字符串是否存在非空字符
        ///// </summary>
        ///// <param name="txt">字符串</param>
        ///// <returns>是否存在非空字符</returns>
        //public static bool HasContent(string txt)
        //{
        //    if (txt == null || txt.Length == 0)
        //        return false;
        //    for (int iCount = 0; iCount < txt.Length; iCount++)
        //    {
        //        if (char.IsWhiteSpace(txt[iCount]) == false)
        //            return true;
        //    }
        //    return false;
        //}

        /// <summary>
        /// 将指定的数据转换为另外一种类型
        /// </summary>
        /// <param name="v">数值</param>
        /// <param name="ValueType">指定的新的数据类型</param>
        /// <returns>转换后的数据</returns>
        public static object GetValue(object v, Type ValueType)
        {
            if (ValueType == null)
                return v;

            if (v == null || DBNull.Value.Equals(v))
                return DBNull.Value;

            if (v is string)
            {
                string txt = (string)v;
                if (ValueType.Equals(typeof(string)))
                {
                    if (txt.Length > 0)
                        return txt;
                }
                if (txt.Trim().Length == 0)
                    return DBNull.Value;
            }
            if (ValueType == null)
            {
                return v;
            }
            try
            {
                if (ValueType.Equals(typeof(string)))
                    return Convert.ToString(v);

                if (ValueType.Equals(typeof(bool)))
                {
                    if (v is String)
                    {
                        string Text = (string)v;
                        Text = Text.Trim();
                        if (Text == "1" || Text == "-1")
                            return true;
                        if (Text == "0")
                            return false;
                        if (string.Compare(Text, "true", true) == 0)
                            return true;
                        if (string.Compare(Text, "false", true) == 0)
                            return false;
                        return DBNull.Value;
                    }
                    return Convert.ToBoolean(v);
                }

                if (ValueType.Equals(typeof(char)))
                    return Convert.ToChar(v);
                if (ValueType.Equals(typeof(byte)))
                    return Convert.ToByte(v);
                if (ValueType.Equals(typeof(sbyte)))
                    return Convert.ToSByte(v);
                if (ValueType.Equals(typeof(short)))
                    return Convert.ToInt16(v);
                if (ValueType.Equals(typeof(ushort)))
                    return Convert.ToUInt16(v);
                if (ValueType.Equals(typeof(int)))
                    return Convert.ToInt32(v);
                if (ValueType.Equals(typeof(uint)))
                    return Convert.ToUInt32(v);
                if (ValueType.Equals(typeof(long)))
                    return Convert.ToInt64(v);
                if (ValueType.Equals(typeof(ulong)))
                    return Convert.ToUInt64(v);
                if (ValueType.Equals(typeof(float)))
                    return Convert.ToSingle(v);
                if (ValueType.Equals(typeof(double)))
                    return Convert.ToDouble(v);
                if (ValueType.Equals(typeof(decimal)))
                {
                    decimal dec = Convert.ToDecimal(Convert.ToSingle(v));// .ToDecimal( v );
                    return dec;

                }
                if (ValueType.Equals(typeof(DateTime)))
                {
                    DateTime dtm = Convert.ToDateTime( v );// Convert.ToDateTime( v );
                    return dtm;
                }
                if (ValueType.Equals(typeof(byte[])))
                {
                    if (v is byte[])
                        return v;
                    if (v is string)
                    {
                        return Convert.FromBase64String((string)v);
                    }
                    return null;
                }
            }
            catch (Exception ext)
            {
#if DEBUG
                System.Windows.Forms.MessageBox.Show(ext.ToString());
#endif
                return DBNull.Value;
            }
            throw new System.Exception("不支持的数据库数据类型:" + ValueType.FullName);
        }


        /// <summary>
        /// 将数值转化为布尔值
        /// </summary>
        /// <param name="v">数值</param>
        /// <returns>布尔值</returns>
        public static bool GetBoolean(object v)
        {
            if (v == null || DBNull.Value.Equals(v))
                return false;
            if (v is bool)
                return (bool)v;
            return Convert.ToBoolean(v);
        }

        /// <summary>
        /// 对象不可实例化
        /// </summary>
        private ValueTypeHelper()
        { }
    }
}
